package bdz.projects.lavender.opsm.ui
{
	import bdz.common.events.GenericEvent;
	import bdz.projects.lavender.opsm.model.HomeItem;
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import com.greensock.TweenMax;
	import com.greensock.easing.*;
	import flash.net.*;
	import flash.external.*;
	import flash.text.TextFormat;
	/**
	 * ...
	 * @author ...
	 */
	public class ImageStripItem extends MovieClip {
		private var _data:HomeItem;
		public var compressedWidth:Number = 210;
		public var expandedWidth:Number; 
		public var expanded:Boolean = false;
		public var index:Number;
		public var exOffset:Number = 185;
		public var dx:Number = 170;
		private var _inited:Boolean = false;
		private var preloader:MovieClip;
		
		function ImageStripItem() {
			buttonMode = useHandCursor = true;
		
			addEventListener(Event.ADDED_TO_STAGE, initializeComponent);
			
		}
		private function initializeComponent(evt:Event):void {
			if (_inited) return;
			
			_inited = true;
			if (_data) {
				applyData();
			}
			display.alpha = 0;
			details.alpha = 0;
			display.smoothenBitmap = true;
			display.addEventListener(Event.COMPLETE, letTheRollBegin);
			createPreloader();
		}
		private function createPreloader():void {
			preloader = new PreloadClip();
			preloader.x = Math.ceil(display.x+(display.width/2));
			preloader.y = Math.ceil(display.y+(display.height / 2));
			addChild(preloader);
		}
		private function letTheRollBegin(evt:Event):void {
			try {
				removeChild(preloader);
			}catch (e:Error) { }
			
			display.width = 210;
			display.height = 113;
			TweenMax.to(display, 1, { alpha:1, ease:Strong.easeInOut } );
			addEventListener(MouseEvent.ROLL_OVER, onDisplayRollOver);
			addEventListener(MouseEvent.CLICK, onDisplayClick);
			addEventListener(MouseEvent.ROLL_OUT, onDisplayRollOut);
		}
		
		private function applyData():void {
			display.source = data.image;
			var boldformat:TextFormat = new TextFormat();
			boldformat.bold = true;
			details.title.text = data.title;
			details.title.cacheAsBitmap = true;
			details.title.setTextFormat(boldformat);
			details.short.cacheAsBitmap = true;
			details.short.text = data.short;
			
			details.x = Math.ceil(details.x);
			display.x = Math.ceil(display.x);
			
			if (expanded) {
				forceExpand();
			}
		}
		private function onDisplayClick(evt:MouseEvent):void {
			if (data.type == "link") {
				navigateToURL(new URLRequest(data.url), data.url.indexOf("http://") != -1 ? "_blank" : "_self");
			}else {
				//call javascript video player here
				ExternalInterface.call("playFlashVideo", data.url, data.title);
			}
			dispatchEvent(new GenericEvent(GenericEvent.CLICKED, data));
		}
		private function onDisplayRollOut(evt:MouseEvent = null):void {
			
		}
		private function onDisplayRollOver(evt:MouseEvent = null):void {
			dispatchEvent(new GenericEvent(GenericEvent.ROLL_OVER, { index:index } ));
			
		}
		public function forceExpand() {
			onDisplayRollOver();
		}
		public function dispatchUpdates(evt:GenericEvent):void {
			dispatchEvent(evt);
			if (preloader && contains(preloader)) {
				try{
					preloader.x = Math.ceil(display.x+(display.width/2));
					preloader.y = Math.ceil(display.y + (display.height / 2));
				}catch(e:Error){trace(e.message)}
				
			}
		}
		public function contract():void {
			
			TweenMax.to(details, .7, { alpha:0, ease:Strong.easeInOut } );
			TweenMax.to(display, .7, { x:0, ease:Strong.easeInOut, onUpdate:dispatchUpdates, onUpdateParams:[new GenericEvent(GenericEvent.CLOSE, { index:index } )] } );

		}
		public function expand():void {
			details.cacheAsBitmap = true;
			display.cacheAsBitmap = true;
			TweenMax.to(details, .7, { alpha:1, ease:Strong.easeInOut } );
			TweenMax.to(display, .7, { x:Math.ceil(dx), ease:Strong.easeInOut, onUpdate:dispatchUpdates, onUpdateParams:[new GenericEvent(GenericEvent.STARTED, { index:index } )] } );
			
			
		}
		public function set data(d:HomeItem):void {
			_data = d;
			if (_inited) {
				applyData();
			}
		}
		public function get data():HomeItem {
			return _data;
		}
	}
	
	
}